<?php

namespace App\HttpController;

use App\Service\SensitiveService;
use App\Service\UserService;
use App\Utility\Verification;
use EasySwoole\HttpAnnotation\AnnotationTag\Api;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiSuccess;
use EasySwoole\HttpAnnotation\AnnotationTag\Method;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;
use EasySwoole\Mysqli\Exception\Exception;
use Throwable;

/**
 * @ApiGroup(groupName="用户信息")
 */
class User extends BaseController
{
    /**
     * @Api(name="获取用户基本信息", path="/user/getInfo", version="1.0.0")
     * @ApiDescription(value="获取用户基本信息")
     * @Method(allow={POST})
     * @ApiSuccess({"code": 200,"result": {"userid": "78620529","phone": "","username": "ꪮriᧁiꪀꪖꪶ .","avatar": "https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eqxTb5ian0mjnKvYQzDMnLPm5g5zo8ZNZapvg0UIZmrRlSlLQzquicSP4cmma0lUVKIbYpxOXfk0cTA/132","house": "","type": 0,"sex": 0,"login_time": 1647853241,"login_ip": "172.18.0.1"},"msg": "获取成功"})
     * @return bool
     * @throws Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws Throwable
     */
    public function getInfo(): bool
    {
        $res = UserService::getInstance()->getInfo($this->uid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="修改用户信息", path="/user/editInfo", version="1.0.0")
     * @ApiDescription(value="修改用户信息")
     * @Method(allow={POST})
     * @Param(name="username", description="用户名", from={JSON})
     * @Param(name="avatar", description="头像", from={JSON})
     * @Param(name="sex", description="性别", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function editInfo($username, $avatar, $sex): bool
    {
        $data = $this->request()->getBody()->__toString();
        $check = SensitiveService::getInstance()->verify($data);
        if (!$check['result']) {
            return $this->writeJsonError($check['msg']);
        }
        $res = UserService::getInstance()->editInfo($this->uid, $username, $avatar, $sex);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取用户房产信息", path="/user/getHouse", version="1.0.0")
     * @ApiDescription(value="获取用户房产信息")
     * @Method(allow={POST})
     * @ApiSuccess({"code": 200,"result": [{"house": "龍舟壹号院1号楼1单元101室","name": "王某","type": 0,"type_value": "业主","default": 1,"status": 1,"time": "1970-01-01"}],"msg": "获取成功"})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getHouse(): bool
    {
        $res = UserService::getInstance()->getHouse($this->uid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="修改用户房产信息", path="/user/editHouse", version="1.0.0")
     * @ApiDescription(value="修改用户房产信息")
     * @Method(allow={POST})
     * @Param(name="name", description="用户姓名", from={JSON})
     * @Param(name="village", description="小区", from={JSON})
     * @Param(name="unit", description="单元", from={JSON})
     * @Param(name="floor", description="楼层", from={JSON})
     * @Param(name="house", description="房号", from={JSON})
     * @Param(name="type", description="住户类型", from={JSON})
     * @return bool
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function editHouse($name, $village, $unit, $floor, $house, $type): bool
    {
        $data = $this->request()->getBody()->__toString();
        $check = SensitiveService::getInstance()->verify($data);
        if (!$check['result']) {
            return $this->writeJsonError($check['msg']);
        }
        $res = UserService::getInstance()->editHouse($this->uid, $name, $village, $unit, $floor, $house, $type);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取用户车位信息", path="/user/getPark", version="1.0.0")
     * @ApiDescription(value="获取用户车位信息")
     * @Method(allow={POST})
     * @ApiSuccess({"code": 200,"result": [{"id": 1,"park": "A101","name": "王某","start_time": "1970-01-01","end_time": "永久","type": 0}],"msg": "获取成功"})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getPark(): bool
    {
        $res = UserService::getInstance()->getPark($this->uid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="修改用户车辆信息", path="/user/editCar", version="1.0.0")
     * @ApiDescription(value="修改用户车辆信息")
     * @Method(allow={POST})
     * @Param(name="park", description="车位号", required="车位不能为空", from={JSON})
     * @Param(name="name", description="姓名", required="姓名不能为空", from={JSON})
     * @Param(name="tel", description="手机号", required="手g机号不能为空", from={JSON})
     * @Param(name="id_card", description="身份证号", required="身份证号不能为空", from={JSON})
     * @Param(name="number", description="车牌号", required="车牌号不能为空", from={JSON})
     * @Param(name="image", description="车辆图片", required="车辆图片不能为空", from={JSON})
     * @Param(name="type", description="类型",  from={JSON})
     * @Param(name="brand", description="品牌",  from={JSON})
     * @Param(name="model", description="车系",  from={JSON})
     * @Param(name="cid", defaultValue="0", description="cid",  from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function editCar($park, $name, $tel, $id_card, $number, $image, $type, $brand, $model, $cid): bool
    {
        $data = $this->request()->getBody()->__toString();
        $check = SensitiveService::getInstance()->verify($data);
        if (!$check['result']) {
            return $this->writeJsonError($check['msg']);
        }
        $checkPhone = Verification::getInstance()->is_phone($tel);
        if (!$checkPhone){
            return $this->writeJsonError('手机号输入错误');
        }
        $res = UserService::getInstance()->editCar($this->uid, $park, $name, $tel, $id_card, $number, $image, $type, $brand, $model, $cid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取用户车辆信息", path="/user/getCar", version="1.0.0")
     * @ApiDescription(value="获取用户车辆信息")
     * @Method(allow={POST})
     * @Param(name="status", defaultValue="99", description="状态：0.未审核，1.审核成功， -1.审核失败", from={JSON})
     * @ApiSuccess({"code": 200,"result": [{"id": 1,"park": "A101","image": "图片","name": "姓名","tel": "手机号","id_card": "身份证","number": "车牌号","type": "类型","brand": "品牌","model": "车系"}],"msg": "获取成功"})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getCar($status): bool
    {
        $res = UserService::getInstance()->getCar($this->uid, $status);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="删除用户车辆信息", path="/user/delCar", version="1.0.0")
     * @ApiDescription(value="删除用户车辆信息")
     * @Method(allow={POST})
     * @Param(name="id", required="", notEmpty="", description="id", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function delCar($id): bool
    {
        $res = UserService::getInstance()->delCar($this->uid, $id);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取用户房间号", path="/user/getFloor", version="1.0.0")
     * @ApiDescription(value="获取用户房间号")
     * @Method(allow={POST})
     * @ApiSuccess({"code": 200,"result": [{"id": 1,"name": "龍舟壹号院1号楼1单元101室"},{"id": 2,"name": "龍舟壹号院1号楼1单元102室"},{"id": 3,"name": "龍舟贰号院2号楼1单元101室"},{"id": 4,"name": "龍舟壹号院2号楼1单元101室"},{"id": 5,"name": "龍舟贰号院2号楼1单元101室"}],"msg": "获取成功"})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getFloor(): bool
    {
        $res = UserService::getInstance()->getFloor($this->uid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="修改房屋默认房间", path="/user/defaultHouse", version="1.0.0")
     * @ApiDescription(value="修改房屋默认房间")
     * @Method(allow={POST})
     * @Param(name="hid", description="房间id", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function defaultHouse($hid): bool
    {
        $res = UserService::getInstance()->defaultHouse($this->uid, $hid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取默认房屋", path="/user/getDefaultHouse", version="1.0.0")
     * @ApiDescription(value="获取默认房屋")
     * @Method(allow={POST})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getDefaultHouse(): bool
    {
        $res = UserService::getInstance()->getDefaultHouse($this->uid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取客服", path="/user/getCaller", version="1.0.0")
     * @ApiDescription(value="获取客服")
     * @Method(allow={POST})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getCaller(): bool
    {
        $res = UserService::getInstance()->getCaller($this->uid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="重置密码", path="/user/changePass", version="1.0.0")
     * @ApiDescription(value="重置密码")
     * @Method(allow={POST})
     * @Param(name="password", required="", notEmpty="密码不能为空", description="原始密码", from={JSON})
     * @Param(name="new_password", required="", notEmpty="新密码不能为空", description="新密码", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function changePass($password, $new_password): bool
    {
        $res = UserService::getInstance()->changePass($this->uid, $password, $new_password);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="获取用户收货地址")
     * @Api(name="获取用户收货地址", path="/user/getAddress", version="1.0.0")
     * @Method(allow={POST})
     * @return bool
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws Throwable
     */
    public function getAddress(): bool
    {
        $res = UserService::getInstance()->getAddress($this->uid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="修改用户收货地址")
     * @Api(name="修改用户收货地址", path="/user/editAddress", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="id", required="", description="收货地址id", notEmpty="收货地址id不能为空", from={JSON})
     * @Param(name="name", required="", description="收货人姓名", notEmpty="收货人姓名不能为空", from={JSON})
     * @Param(name="phone", required="", description="收货人电话号码", notEmpty="收货人电话号码不能为空", from={JSON})
     * @Param(name="address", type="array", required="", description="收货地址", notEmpty="收货地址不能为空", from={JSON})
     * @Param(name="address_detail", description="详细收货地址", required="", from={JSON})
     * @Param(name="label", required="", description="是否默认：0.否， 1.是", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws Exception
     */
    public function editAddress($phone): bool
    {
        $data = $this->request()->getBody()->__toString();
        $check = SensitiveService::getInstance()->verify($data);
        if (!$check['result']) {
            return $this->writeJsonError($check['msg']);
        }
        $checkPhone = Verification::getInstance()->is_phone($phone);
        if (!$checkPhone){
            return $this->writeJsonError('手机号输入错误');
        }
        $data = $this->request()->getBody()->__toString();
        $res = UserService::getInstance()->editAddress($this->uid, $data);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="删除收货地址")
     * @Api(name="删除收货地址", path="/user/delAddress", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="rid", required="", description="收货地址id", notEmpty="收货地址id不能为空", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws Exception
     */
    public function delAddress($rid): bool
    {
        $res = UserService::getInstance()->delAddress($this->uid, $rid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="收货地址默认设置")
     * @Api(name="收货地址默认设置", path="/user/editAddressLabel", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="rid", required="", description="收货地址id", notEmpty="收货地址id不能为空", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws Exception
     */
    public function editAddressLabel($rid): bool
    {
        $res = UserService::getInstance()->editAddressLabel($this->uid, $rid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="邀请家人")
     * @Api(name="邀请家人", path="/user/family", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="name", required="", description="姓名", notEmpty="姓名不能为空", from={JSON})
     * @Param(name="tel", required="", description="手机号", notEmpty="手机号不能为空", from={JSON})
     * @Param(name="type", required="", description="业主类型：0.家属，1.租户：", notEmpty="类型不能为空", from={JSON})
     * @return bool
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function family($name, $tel, $type): bool
    {
        $data = $this->request()->getBody()->__toString();
        $check = SensitiveService::getInstance()->verify($data);
        if (!$check['result']) {
            return $this->writeJsonError($check['msg']);
        }
        $checkPhone = Verification::getInstance()->is_phone($tel);
        if (!$checkPhone){
            return $this->writeJsonError('手机号输入错误');
        }
        $res = UserService::getInstance()->family($this->uid, $name, $tel, $type);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="获取我的家人")
     * @Api(name="获取我的家人", path="/user/getFamily", version="1.0.0")
     * @Method(allow={POST})
     * @return bool
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getFamily(): bool
    {
        $res = UserService::getInstance()->getFamily($this->uid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="删除我的家人")
     * @Api(name="删除我的家人", path="/user/delFamily", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="id", required="", description="id", notEmpty="id不能为空", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function delFamily($id): bool
    {
        $res = UserService::getInstance()->delFamily($this->uid, $id);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="提交已读房产交房通知")
     * @Api(name="提交房产交房通知", path="/user/submitDeliveryRead", version="1.0.0")
     * @Method(allow={POST})
     * @return bool
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function submitDeliveryRead(): bool
    {
        $res = UserService::getInstance()->submitDeliveryRead($this->uid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }
}